const conn = require("../data");

// 页面渲染
module.exports.hot = (req, res) => {
  conn.query(`select * from hotTags where id = ?`, [req.params.id], (error, data) => {
    if (error) return console.log(error);
    conn.query('select page_news_id from hotTags where name = ?', [data[0].name], (error2, data2) => {
      if (error2) return console.log(error2);
      let arr = [];
      for (let i = 0; i < data2.length; i++) {
        arr.push(data2[i].page_news_id)
      };
      conn.query(`select *,date_format(now(), '%Y-%m') date , date_format(now(), '%m') month from page_news where id in (?);
      select distinct name,id from hotTags group by name;
      select * from order_product order by page_id limit 4;
      select *,date_format(now(), '%Y-%m-%d %H-%m-%s') date from page_news order by page_options_id limit 20
      `, [arr], (error3, data3) => {
          if (error3) return console.log(error3);
          return res.render('hot', { list: data3[1], name: data[0].name, arr: data3[0], image: data3[2], info: data3[3] });
        })
    })
  })
}
// 热门标签信息 渲染
module.exports.showMore = (req, res) => {
  let Start = (req.query.pageNum - 1) * req.query.pageSize;
  conn.query('select  page_news_id from hotTags where  name = (select name from hotTags where id = ?)', [req.query.id], (error, data) => {
    if (error) return console.log(error);
    let arr = [];
    for (let i = 0; i < data.length; i++) {
      arr.push(data[i].page_news_id);
    }
    conn.query(`select count(*) num  from page_news where id in (?) limit ${Start},${req.query.pageSize};
    select *  from page_news where id in (?) limit ${Start},${req.query.pageSize}`, [arr, arr], (error2, data2) => {
        if (error2) return console.log(error2);
        res.json(data2)
      })
  })
}